package m.system.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import m.common.model.Model;
import m.common.model.util.ModelUtil;
import m.system.db.DataSet;
import m.system.exception.MException;
import m.system.lang.HtmlBodyContent;
import m.system.lang.IJsonOuter;

import java.lang.reflect.Array;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ObjectUtil {
	/**
	 * 判断模型主键是否为空
	 */
	public static boolean isEmptyOid(Model model){
		if(null==model){
			return true;
		}
		return StringUtil.isSpace(model.getOid());
	}
	/**
	 * 判断模型主键是否为非空
	 */
	public static boolean notEmptyOid(Model model){
		return !isEmptyOid(model);
	}

	/**
	 * 对象内部字符串全部去掉两端空格
	 */
	public static <T> T trim(T object){
		if(null==object) return null;
		else if(object instanceof String) return (T) ((String) object).trim();
		else if(object instanceof JSONObject){
			JSONObject jsonObject=(JSONObject) object;
			for (Map.Entry<String, Object> next : jsonObject.entrySet()) {
				Object value = next.getValue();
				if (value != null) {
					if (value instanceof String) {
						//清空值前后空格
						jsonObject.put(next.getKey(), trim(value));
					} else if (value instanceof JSONObject) {
						trim((JSONObject) value);
					} else if (value instanceof JSONArray) {
						trim((JSONArray) value);
					}
				}
			}
		}else if(object instanceof JSONArray){
			JSONArray array=(JSONArray)object;
			if (!array.isEmpty()) {
				for (int i = 0; i < array.size(); i++) {
					Object obj = array.get(i);
					if (obj != null) {
						if (obj instanceof String) {
							array.set(i, trim(obj));
						} else if (obj instanceof JSONObject) {
							trim((JSONObject) obj);
						} else if (obj instanceof JSONArray) {
							trim((JSONArray) obj);
						}
					}
				}
			}
		}
		return object;
	}
	/**
	 * 获取model的oid
	 */
	public static String getOid(Model model){
		if(null==model){
			return null;
		}
		return model.getOid();
	}
	@SuppressWarnings("unchecked")
	public static <T> Object convert(Class<T> clazz,Object object){
		if(null==object){
			return null;
		}else if(object.getClass().equals(clazz)){
			return object;
		}else if(String.class.equals(clazz)){
			return object.toString();
		}else if(Byte.class.equals(clazz)){
			return Byte.parseByte(object.toString());
		}else if(clazz.equals(byte.class)){
			return Byte.parseByte(object.toString());
		}else if(Short.class.equals(clazz)){
			return Short.parseShort(object.toString());
		}else if(clazz.equals(short.class)){
			return Short.parseShort(object.toString());
		}else if(Integer.class.equals(clazz)){
			return Integer.parseInt(object.toString());
		}else if(clazz.equals(int.class)){
			return Integer.parseInt(object.toString());
		}else if(Long.class.equals(clazz)){
			return Long.parseLong(object.toString());
		}else if(clazz.equals(long.class)){
			return Long.parseLong(object.toString());
		}else if(Float.class.equals(clazz)){
			return Float.parseFloat(object.toString());
		}else if(clazz.equals(float.class)){
			return Float.parseFloat(object.toString());
		}else if(Double.class.equals(clazz)){
			return Double.parseDouble(object.toString());
		}else if(clazz.equals(double.class)){
			return Double.parseDouble(object.toString());
		}else if(Boolean.class.equals(clazz)){
			return Boolean.parseBoolean(object.toString());
		}else if(clazz.equals(boolean.class)){
			return Boolean.parseBoolean(object.toString());
		}else if(Date.class.equals(clazz)||java.sql.Date.class.equals(clazz)){
			String string=object.toString().replace('T', ' ').replace(',', ' ');
			return DateUtil.format(string, "yyyy-MM-dd HH:mm:ss.SSS".substring(0,string.length()));
		}else if(Model.class.isAssignableFrom(clazz)&&object.getClass().equals(JSONObject.class)){
			return JSONObject.toJavaObject((JSONObject)object, clazz);
		}else if(clazz.isArray()){
			Object[] objs;
			if(object.getClass().equals(JSONArray.class)) {
				objs=((JSONArray)object).toJavaList(clazz.getComponentType()).toArray();
			}else {
				objs=(Object[]) object;
			}
			Class<?> et = clazz.getComponentType();
			T[] arr=(T[]) Array.newInstance(et, objs.length);
			for(int i=0;i<objs.length;i++) {
				arr[i]=(T) convert(et,objs[i]);
			}
			return arr;
		}else {
			if(object instanceof JSONObject||object instanceof JSONMessage) {
				return JSONObject.parseObject(object.toString(), clazz);
			}else {
				return object;
			}
		}
	}
	/**
	 * 转换对象为字符串的展现形式.
	 *  value
	 * 
	 *  MException 
	 */
	@SuppressWarnings("unchecked")
	public static String toString(Object value) throws MException{
		if(null==value){
			return "null";
		}else if(value instanceof HtmlBodyContent){
			return value.toString();
		}else if(value instanceof Number || value instanceof Boolean){
			return value.toString();
		}else if(value instanceof Date){
			return "\"" + DateUtil.format((Date) value, DateUtil.YYYY_MM_DD_HH_MM_SS) + "\"";
		}else if(value instanceof Object[]){
			return ArrayUtil.toString((Object[])value);
		}else if(value instanceof List){
			return ArrayUtil.toString((List<Object>)value);
		}else if(value instanceof IJsonOuter){
			return ((IJsonOuter)value).toJSONString();
		}else if(value instanceof Map){
			return new JSONMessage((Map<String, Object>) value).toJSONString();
		}else if(value instanceof Model){
			return ModelUtil.toJSONMessage("",(Model)value).toJSONString();
		}else if(value instanceof DataSet){
			return ArrayUtil.toString(((DataSet) value).toArray());
		}else{
			return "\"" + StringUtil.conver2JS(value.toString()) + "\"";
		}
	}
	public static void main(String[] a) throws MException{
		//convert(String.class,1);
		SystemUtil.println(toString(new String[]{"111",""}));
	}
}
